﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Styx;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.Helpers;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Action = Styx.TreeSharp.Action;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Styx.Pathing;
using Styx.Common;
using System.Windows.Media;
using Styx.CommonBot.Frames;
using System.Diagnostics;

using F = Hunter.GUI.Form1;
using T = Hunter.Helpers.targets;
using U = Hunter.Helpers.Unit;
using EQ = Hunter.Helpers.UseItems;
using M = Hunter.Helpers.Movement;
using CS = Hunter.Spells.SpellCasting;
using HKM = Hunter.Helpers.HotkeyManager;
using CRS = Hunter.CRSettings.CRSettings;
using I = Hunter.Helpers.Interrupts;
using System.IO;


namespace Hunter.Rotation
{
    class BMRotation
    {
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static string usedBot { get { return BotManager.Current.Name.ToUpper(); } }

        public static Composite Rotation()
        {
            return new FrameLockSelector(
                new Decorator(ret => Me.GotTarget && Me.IsChanneling || (Me.Mounted && !AutoBot) || HKM.pauseRoutineOn || HKM.manualOn, Hold()),
                new Decorator(ret => Me.GotTarget && T.RangeAddCount >= 3 && !HKM.aoeStop && SpellManager.HasSpell(CS.MULTI_SHOT),
                    new FrameLockSelector(
                        CS.castMisdirection(),
                        CS.CastCounterShot(),
                        CS.castFocusFire(),
                        CS.castKillShot(),
                        CS.castKillCommand(),
                        CS.castBestialWrath(),
                        new Decorator(ret => Me.GotTarget && Me.MovementInfo.MovingBackward && Me.IsSafelyFacing(Me.CurrentTarget), CS.CastIceTrapOnMe()),
                        CS.castMultiShot(),
                        CS.CastExplosiveTrap(),
                        CS.CastCobraShot()
                        )),
                new Decorator(ret => Me.GotTarget && (T.RangeAddCount < 3 || HKM.aoeStop || !SpellManager.HasSpell(CS.MULTI_SHOT)),
                    new FrameLockSelector(
                        CS.castMisdirection(),
                        CS.CastCounterShot(),
                        CS.CastWyvernSting(),
                        CS.castMisdirection(),
                        CS.castFocusFire(),
                        CS.castKillShot(),
                        CS.castKillCommand(),
                        CS.castBestialWrath(),
                        new Decorator(ret => Me.GotTarget && Me.CurrentTarget.Distance <= 39 && useMyTrinket1, EQ.useTrinket1()),
                        new Decorator(ret => Me.GotTarget && Me.CurrentTarget.Distance <= 39 && useMyTrinket2, EQ.useTrinket2()),
                        new Decorator(ret => Me.GotTarget && Me.CurrentTarget.Distance <= 39 && useMyGloves, EQ.useGloves()),
                        CS.CastBloodfury(),
                        CS.CastBerserking(),
                        new Decorator(ret => Me.GotTarget && Me.MovementInfo.MovingBackward && Me.IsSafelyFacing(Me.CurrentTarget), CS.CastConcussiveShot()),
                        new Decorator(ret => Me.GotTarget && Me.CurrentTarget.IsPlayer, CS.CastConcussiveShot()),
                        new Decorator(ret => Me.GotTarget && Me.MovementInfo.MovingBackward && Me.IsSafelyFacing(Me.CurrentTarget), CS.CastIceTrapOnMe()),
                        new Decorator(ret => Me.GotTarget && T.RangeAddCount > 2, CS.CastExplosiveTrap()),
                        CS.CastMurderOfCrows(),
                        CS.CastStampede(),
                        CS.CastDireBeast(),
                        CS.CastBarrage(),
                        CS.CastPowerShot(),
                        CS.CastGlaiveToss(),
                        CS.CastArcaneShotBM(),
                        CS.CastCobraShot()


                )));
        }

        #region AutoBot
        private static bool AutoBot
        {
            get
            {
                return usedBot.Contains("QUEST") || usedBot.Contains("GRIND") || usedBot.Contains("GATHER") || usedBot.Contains("ANGLER") || usedBot.Contains("ARCHEO");

            }
        }
        #endregion

        #region autoattack
        private static Composite AutoAttack()
        {
            return new Action(ret =>
            {
                Lua.DoString("StartAttack()");
                return RunStatus.Failure;
            });
        }
        #endregion

        #region Hold
        private static Composite Hold()
        {
            return new Action(ret =>
            {
                return;
            });
        }
        #endregion

        #region framelock
        private class FrameLockSelector : PrioritySelector
        {
            public FrameLockSelector(params Composite[] children)
                : base(children)
            {
                /*empty*/
            }


            public FrameLockSelector(ContextChangeHandler contextChange, params Composite[] children)
                : base(contextChange, children)
            {
                /*empty*/
            }


            public override RunStatus Tick(object context)
            {
                using (StyxWoW.Memory.AcquireFrame())
                {
                    return base.Tick(context);
                }
            }
        }
        #endregion

        #region trinkets, gloves, racials
        public static bool useMyTrinket1
        {
            get
            {
                if (CRS.myPrefs.Trinket1 == 1) { return false; }
                if (T.IsWoWBoss(Me.CurrentTarget) && CRS.myPrefs.Trinket1 == 2 && !CRS.myPrefs.CDTrinket1) { return true; }
                if (CRS.myPrefs.Trinket1 == 2 && CRS.myPrefs.CDTrinket1 && HKM.cooldownsOn) { return true; }
                if (CRS.myPrefs.Trinket1 == 2 && !CRS.myPrefs.CDTrinket1) { return true; }
                if (CRS.myPrefs.Trinket1 == 3 && Me.HealthPercent <= CRS.myPrefs.PercentTrinket1HP) { return true; }

                return false;
            }
        }
        public static bool useMyTrinket2
        {
            get
            {
                if (CRS.myPrefs.Trinket2 == 1) { return false; }
                if (T.IsWoWBoss(Me.CurrentTarget) && CRS.myPrefs.Trinket2 == 2 && !CRS.myPrefs.CDTrinket2) { return true; }
                if (CRS.myPrefs.Trinket2 == 2 && CRS.myPrefs.CDTrinket2 && HKM.cooldownsOn) { return true; }
                if (CRS.myPrefs.Trinket2 == 2 && !CRS.myPrefs.CDTrinket2) { return true; }
                if (CRS.myPrefs.Trinket2 == 3 && Me.HealthPercent <= CRS.myPrefs.PercentTrinket2HP) { return true; }

                return false;
            }
        }
        public static bool useMyGloves
        {
            get
            {
                if (CRS.myPrefs.Gloves == 1) { return false; }
                if (T.IsWoWBoss(Me.CurrentTarget) && CRS.myPrefs.Gloves == 2 && !CRS.myPrefs.CDGloves) { return true; }
                if (CRS.myPrefs.Gloves == 2 && CRS.myPrefs.CDGloves && HKM.cooldownsOn) { return true; }
                if (CRS.myPrefs.Gloves == 2 && !CRS.myPrefs.CDGloves) { return true; }

                return false;
            }
        }
        public static bool useMyBloodfury
        {
            get
            {
                if (!CRS.myPrefs.Bloodfury) { return false; }
                if (T.IsWoWBoss(Me.CurrentTarget) && !CRS.myPrefs.CDRacial) { return true; }
                if (CRS.myPrefs.Bloodfury && CRS.myPrefs.CDRacial && HKM.cooldownsOn) { return true; }
                if (CRS.myPrefs.Bloodfury && !CRS.myPrefs.CDRacial) { return true; }

                return false;
            }
        }
        public static bool useMyBerserking
        {
            get
            {
                if (!CRS.myPrefs.Berserking) { return false; }
                if (T.IsWoWBoss(Me.CurrentTarget) && !CRS.myPrefs.CDRacial) { return true; }
                if (CRS.myPrefs.Berserking && CRS.myPrefs.CDRacial && HKM.cooldownsOn) { return true; }
                if (CRS.myPrefs.Berserking && !CRS.myPrefs.CDRacial) { return true; }

                return false;
            }
        }
        public static bool useMyStampede
        {
            get
            {
                if (T.IsWoWBoss(Me.CurrentTarget) && !CRS.myPrefs.CDStampede) { return true; }
                if (CRS.myPrefs.CDStampede && HKM.cooldownsOn) { return true; }
                return false;
            }
        }
        public static bool useMyMurder
        {
            get
            {
                if (T.IsWoWBoss(Me.CurrentTarget) && !CRS.myPrefs.CDMurder) { return true; }


                return false;
            }
        }
        #endregion
    }
}
